package com.example.administrator.personalmail.adapter;

import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.personalmail.R;
import com.example.administrator.personalmail.model.Wares;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

public class CategorySecondAdapter extends RecyclerView.Adapter {
    private List<Wares> mDatas;
    private int NORMAL = 0;
    private int FOOTER = 1;
    private boolean isNeedFooter;
    private View view;
    private View footer;

    public CategorySecondAdapter(List<Wares> wares){
        mDatas = wares;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       if (viewType==FOOTER && isNeedFooter){
            footer = LayoutInflater.from(parent.getContext()).inflate(R.layout.no_more,parent,false);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            footer.setLayoutParams(lp);
            return new ViewHolder(footer);
        }

        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.template_grid_wares,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (getItemViewType(position)==FOOTER) return;

        if (getItemViewType(position)==NORMAL){
            final ViewHolder holder = (ViewHolder) viewHolder;
            Wares wares = mDatas.get(position);

            holder.draweeView.setImageURI(Uri.parse(wares.getImgUrl()));
            holder.textTitle.setText(wares.getName());
            holder.textPrice.setText("￥ "+wares.getPrice());
        }


    }

    @Override
    public int getItemViewType(int position) {
        if (position==mDatas.size() && isNeedFooter){
            return FOOTER;
        }
        return NORMAL;
    }

    public void setFOOTER(boolean type){
        isNeedFooter = type;
    }


    public int getSize(){
        return mDatas.size();
    }


    public void clearData(){

        mDatas.clear();
        notifyItemRangeRemoved(0,mDatas.size());
    }

    public void addData(List<Wares> datas){

        addData(0,datas);
    }

    public void addData(int position,List<Wares> datas){

        if(datas !=null && datas.size()>0) {

            mDatas.addAll(datas);
            notifyItemRangeChanged(position, mDatas.size());
        }
    }


    @Override
    public int getItemCount() {
        if (isNeedFooter)
            return mDatas.size()+1;

        return mDatas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        SimpleDraweeView draweeView;
        TextView textTitle;
        TextView textPrice;

        public ViewHolder(View itemView) {
            super(itemView);
            draweeView = (SimpleDraweeView) itemView.findViewById(R.id.fv_category_second);
            textTitle= (TextView) itemView.findViewById(R.id.tvtitle_category_second);
            textPrice= (TextView) itemView.findViewById(R.id.tv_price_category_second);

        }
    }
}
